package com.qujia.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.system.domain.*;
import com.qujia.system.domain.dto.SysMemberManagementQueryDto;
import com.qujia.system.domain.vo.SysEnterpriseAndStationVo;
import com.qujia.system.domain.vo.SysMemberManagementVo;
import com.qujia.system.mapper.SysMemberManagementMapper;
import com.qujia.system.mapper.SysStationUserMapper;
import com.qujia.system.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 油站信息Service业务层处理
 * 
 * @author qujia
 * @date 2020-12-08
 */
@Service
public class SysStationUserServiceImpl  extends ServiceImpl<SysStationUserMapper, SysStationUser> implements  ISysStationUserService
{
    @Autowired
    private SysStationUserMapper sysStationUserMapper;
    @Autowired
    private SysTeamManagementService sysTeamManagementService;

    @Autowired
    private SysMemberManagementService sysMemberManagementService;

    @Resource
    private SysMemberManagementMapper managementMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysEnterpriseUserService enterpriseUserService;

    @Autowired
    private ISysStationUserService stationUserService;


    /**
     * 查询油站信息
     *
     * @param stationId 油站信息ID
     * @return 油企信息
     */
    public SysStationUser selectSysStationUserById(Long stationId){
        return sysStationUserMapper.selectSysStationUserById(stationId);
    }

    /**
     * 查询油站信息列表
     *
     * @param station 油站信息
     * @return 油站信息集合
     */
    public List<SysStationUser> selectSysStationUserList( SysStationUser station){
        return sysStationUserMapper.selectSysStationUserList(station);
    }

    /**
     * 查询油站id的集合通过油企
     *
     * @param enterpriseId 油企id信息
     * @return 油站信息集合
     */
    public List<String> selectStationIdByEnterpriseId( Long enterpriseId){
        List<String> ids=new ArrayList<>();
        SysStationUser stationUser=new SysStationUser();
        stationUser.setEnterpriseId(enterpriseId);
        List<SysStationUser> stationUsers=sysStationUserMapper.selectSysStationUserList(stationUser);
        for(SysStationUser sysStationUser: stationUsers){
            ids.add(sysStationUser.getStationId()+"");
        }
        return ids;
    }

    /**
     * 新增油站信息
     *
     * @param station 油站信息
     * @return 结果
     */
    public int insertSysStationUser(SysStationUser station){
        return sysStationUserMapper.insertSysStationUser(station);
    }

    /**
     * 修改油站信息
     *
     * @param station 油企信息
     * @return 结果
     */
    public int updateSysStationUser(SysStationUser station){
        return sysStationUserMapper.updateSysStationUser(station);
    }

    /**
     * 删除油站信息
     *
     * @param stationId 油企信息ID
     * @return 结果
     */
    public int deleteSysStationUserById(Long stationId){
        return sysStationUserMapper.deleteSysStationUserById(stationId);
    }

    /**
     * 批量删除油站信息
     *
     * @param stationIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteSysStationUserByIds(Long[] stationIds){
        return sysStationUserMapper.deleteSysStationUserByIds(stationIds);
    }

    @Override
    public SysEnterpriseAndStationVo getItemIds(SysUser sysUser) {
        SysEnterpriseAndStationVo item = new SysEnterpriseAndStationVo();
        item.setUserList(new ArrayList<>());
        List<SysEnterpriseUser> enterpriseList = new ArrayList<>();
        List<SysStationUser> sysStationList = new ArrayList<>();
        //区域经理
        if (StringUtils.equals(UserConstants.LEVEL_FIVE, sysUser.getUserLevel())) {
            //查询团队
            SysTeamManagement one =
                    sysTeamManagementService.getOne(new QueryWrapper<SysTeamManagement>().eq("leader_id", sysUser.getUserId()).eq("status",0));
            List<String> strings = null;
            if (one != null ) {
                List<String> enterpriseIds = new ArrayList<>();
                String members = one.getMembers();

                if(members != null && !members.isEmpty()){
                    strings = Arrays.asList(one.getMembers().split(","));
                }else{
                    throw new BaseException("未创建油企");
                }
                SysUser user = new SysUser();
                user.setUserIdList(strings.stream().map(Long::parseLong).collect(Collectors.toList()));
                List<SysUser> sysUsers = sysUserService.selectUserList(user);
                if (sysUsers.size()>0) {
                    for(SysUser sysUserItem:sysUsers){
                        SysMemberManagementQueryDto memberManagementDto = new SysMemberManagementQueryDto();
                        memberManagementDto.setSalesmanId(sysUserItem.getUserId());
                        memberManagementDto.setStatus(0);
                        List<SysMemberManagementVo> aMemberManagementQueryDto = managementMapper.selectMemberList(memberManagementDto);
                        for(SysMemberManagementVo aMemberManagementVo:aMemberManagementQueryDto){
                            if(aMemberManagementVo.getEnterpriseIds()!=null){
                                List<String> strings1 = Arrays.asList(aMemberManagementVo.getEnterpriseIds().split(","));
                                enterpriseIds.addAll(strings1);
                            }
                        }

                    }
                }
                //查询成员
                List<SysMemberManagement> managements =
                        sysMemberManagementService.list(new QueryWrapper<SysMemberManagement>()
                                .in("salesman_id", strings));
                //查询油企
                if (managements != null && managements.size()>0) {
                    List<String> collect =
                            managements.stream().map(SysMemberManagement::getEnterpriseId).collect(Collectors.toList());
                    for (String s : collect) {
                        List<String> strings1 = Arrays.asList(s.split(","));
                        enterpriseIds.addAll(strings1);
                    }
                }else{
                    throw new BaseException("未创建油企");
                }
                enterpriseList = enterpriseUserService.list(new QueryWrapper<SysEnterpriseUser>()
                        .in("enterprise_id", enterpriseIds)
                        .eq("delete_status", "0"));
            }else{
                throw new BaseException("未创建油企");
            }
        }
        //业务员
        if (StringUtils.equals(UserConstants.LEVEL_SIX, sysUser.getUserLevel())) {
            SysMemberManagement managements =
                    sysMemberManagementService.getOne(new QueryWrapper<SysMemberManagement>().eq("salesman_id", sysUser.getUserId()));
            if (managements != null && StringUtils.isNotEmpty(managements.getEnterpriseId())) {
                List<String> strings1 = Arrays.asList(managements.getEnterpriseId().split(","));
                enterpriseList =
                        enterpriseUserService.list(new QueryWrapper<SysEnterpriseUser>()
                                .in("enterprise_id", strings1)
                                .eq("delete_status", "0"));
            }else{
                throw new BaseException("未创建油企");
            }
        }
        //合伙人
        if (StringUtils.equals(UserConstants.LEVEL_SEVEN, sysUser.getUserLevel())) {
            SysUser user = new SysUser();
            user.setSupremeLevelId(sysUser.getUserId());
            List<SysUser> sysUsers = sysUserService.selectUserList(user);
            if (sysUsers.size()>0) {
                List<Long> collect = sysUsers.stream().map(SysUser::getEnterpriseId).collect(Collectors.toList());
                enterpriseList = enterpriseUserService.list(new QueryWrapper<SysEnterpriseUser>()
                        .in("enterprise_id", collect)
                        .eq("delete_status", "0"));
                if (enterpriseList.size()>0) {
                    List<Long> collect1 = enterpriseList.stream().map(SysEnterpriseUser::getEnterpriseId).collect(Collectors.toList());
                    sysStationList.addAll(stationUserService.list(new QueryWrapper<SysStationUser>().in("enterprise_id", collect1)));
                }else{
                    throw new BaseException("未创建油企");
                }
                List<Long> collect1 = sysUsers.stream().map(SysUser::getStationId).collect(Collectors.toList());
                List<SysStationUser> sysStations = stationUserService.list(new QueryWrapper<SysStationUser>().in("enterprise_id", collect1));
                sysStationList.addAll(sysStations);
            }else{
                throw new BaseException("未创建油企");
            }

        }
        if (enterpriseList != null && enterpriseList.size() > 0) {
            List<Long> enterpriseIds = enterpriseList.stream().map(SysEnterpriseUser::getEnterpriseId).collect(Collectors.toList());
            sysStationList.addAll(stationUserService.list(new QueryWrapper<SysStationUser>().in("enterprise_id", enterpriseIds)));
            List<Long> stationIds = sysStationList.stream().map(SysStationUser::getStationId).collect(Collectors.toList());
            item.setStationIds(stationIds);
            item.setEnterpriseIds(enterpriseIds);
            List<String> collect = stationIds.stream().map(String::valueOf).collect(Collectors.toList());
            item.setStationIdsStr(collect);
        }
        return item;
    }
}
